package configure;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InputConfig {
    //mongodb的导入配置
    //mongodb的ip
    public static String HOST = "127.0.0.1";
    //端口号
    public static int PORT = 27017;
    //mongodb的用户、密码
    public static String USERNAME = "";
    public static String PASSWORD = "";
    //mongodb存放数据的库
    public static String DATABASE = "";
    //mongodb存放case数据的collection
    public static String COLLECTION_CASE = "";
    //mongodb存放target数据的collection
    public static String COLLECTION_TARGET = "";
    //mongo.case导入文件的存放路径
    public static String MONGO_FILEPATH_CASE = "";
    //mongo.case导入文件名
    public static String MONGO_FILENAME_CASE = "";
    //mongo.target导入文件的存放路径
    public static String MONGO_FILEPATH_TARGET = "";
    //mongo.target导入文件名
    public static String MONGO_FILENAME_TARGET = "";
    //mysql的导入配置
    //目标mysql
    public static String DBName = "test";
    public static String DB_URL = "";
    //用户名和密码
    public static String USER = "";
    public static String PASS = "";
    //导出文件路径
    public static String MYSQL_FILEPATH = "";
    //导出文件名
    public static String MYSQL_FILENAME = "";
    //properties文件路径
    public static String PROPERTIES_PATH = "";
    //新的ownerid
    public static String OWNER_ID = "";

    public static void main() {
        try {
            readInputConfig();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean readInputConfig() throws Exception {
        File file = new File("input-config.txt");
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
            //BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                if (!s.trim().startsWith("#")) {
                    String key = getKey(s.trim());
                    String value = getValue(s.trim());
                    setKeyAndValue(key, value);
                    System.out.println(key + ":" + value);
                }
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public static String getKey(String s) {
        String key = "";
        Matcher m = Pattern.compile("(\\[[^\\]]*\\])").matcher(s);
        //获取key
        if (m.find()) {
            key = m.group();
        }
        return key;
    }

    public static String getValue(String s) {
        String value = "";
        Matcher m = Pattern.compile("\"(.*?)\"").matcher(s);
        //获取value
        if (m.find()) {
            value = m.group().substring(1, m.group().length() - 1);
        }
        return value;
    }

    public static void setKeyAndValue(String key, String value) {
        switch (key) {
            case "[mongodb.HOST]":
                HOST = value;
                break;
            case "[mongo.PORT]":
                PORT = Integer.parseInt(value);
                break;
            case "[mongodb.USERNAME]":
                USERNAME = value;
                break;
            case "[mongodb.PASSWORD]":
                PASSWORD = value;
                break;
            case "[mongodb.DATABASE]":
                DATABASE = value;
                break;
            case "[mongodb.COLLECTION_CASE]":
                COLLECTION_CASE = value;
                break;
            case "[mongodb.COLLECTION_TARGET]":
                COLLECTION_TARGET = value;
                break;
            case "[mongodb.MONGO_FILEPATH_CASE]":
                MONGO_FILEPATH_CASE = value;
                break;
            case "[mongodb.MONGO_FILENAME_CASE]":
                MONGO_FILENAME_CASE = value;
                break;
            case "[mongodb.MONGO_FILEPATH_TARGET]":
                MONGO_FILEPATH_TARGET = value;
                break;
            case "[mongodb.MONGO_FILENAME_TARGET]":
                MONGO_FILENAME_TARGET = value;
                break;
            case "[mysql.DBName]":
                DBName = value;
                break;
            case "[mysql.DB_URL]":
                DB_URL = value;
                break;
            case "[mysql.USER]":
                USER = value;
                break;
            case "[mysql.PASS]":
                PASS = value;
                break;
            case "[mysql.MYSQL_FILEPATH]":
                MYSQL_FILEPATH = value;
                break;
            case "[mysql.MYSQL_FILENAME]":
                MYSQL_FILENAME = value;
                break;
            case "[mysql.PROPERTIES_PATH]":
                PROPERTIES_PATH = value;
                break;
            case "[mysql.OWNER_ID]":
                OWNER_ID = value;
                break;
            default:
                break;
        }
    }
}
